{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139 ns ± 4.89 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "l = [1, 2, 3, 4, 5, 6, 7]\n",
    "for item in l: # l 可以是元组、集合、字典（key）等可迭代对象\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index = 0 item = 1\n",
      "index = 1 item = 2\n",
      "index = 2 item = 3\n",
      "index = 3 item = 4\n",
      "index = 4 item = 5\n",
      "index = 5 item = 6\n",
      "index = 6 item = 7\n"
     ]
    }
   ],
   "source": [
    "l = [1, 2, 3, 4, 5, 6, 7]\n",
    "for index, item in enumerate(l):\n",
    "    print(f\"{index = } {item = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "407 ns ± 10.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit \n",
    "l = [1, 2, 3, 4, 5, 6, 7]\n",
    "index = 0\n",
    "max_index = len(l) - 1\n",
    "\n",
    "while index <= max_index:\n",
    "    pass\n",
    "    index += 1  # index += 1 相当于 index = new int(index + 1)）\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "****************************************\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    if i < 5:\n",
    "        continue\n",
    "    print(i)\n",
    "    \n",
    "print(\"****\"*10)\n",
    "i = 1\n",
    "while i < 10:\n",
    "    if i < 5:       \n",
    "        i += 1\n",
    "        continue\n",
    "    print(i)\n",
    "    i += 1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "****************************************\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    if i  == 5:\n",
    "        break\n",
    "    print(i)\n",
    "    \n",
    "print(\"****\"*10)\n",
    "i = 1\n",
    "while i < 10:\n",
    "    if i == 5:       \n",
    "        break\n",
    "    print(i)\n",
    "    i += 1 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如果遍历一个已知的集合，那么使用 for。\n",
    "\n",
    "### 如果要在满足某个条件前，不停地重复某些操作，并且没有特定的集合，那么使用 while 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter your questions, enter \"q\" to exit123\n",
      "123\n",
      "Please enter your questions, enter \"q\" to exit131234\n",
      "131234\n",
      "Please enter your questions, enter \"q\" to exitdasdf\n",
      "dasdf\n",
      "Please enter your questions, enter \"q\" to exitq\n",
      "Exit system\n"
     ]
    }
   ],
   "source": [
    "\n",
    "while True:\n",
    "    try:\n",
    "        text = input('Please enter your questions, enter \"q\" to exit')\n",
    "        if text == 'q':\n",
    "            print('Exit system')\n",
    "            break\n",
    "        ...\n",
    "        ...\n",
    "        print(text)\n",
    "    except Exception as err:\n",
    "        print('Encountered error: {}'.format(err))\n",
    "        break "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "expression1 if condition else expression2 for item in iterable\n",
    "\n",
    "for item in iterable:\n",
    "    if condition:\n",
    "        expression1\n",
    "    else:\n",
    "        expression2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['奇数', '偶数', '奇数', '偶数', '奇数', '偶数', '奇数']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, 3, 4, 5, 6, 7]\n",
    "\n",
    "['偶数' if item % 2 == 0 else '奇数' for item in l]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in l:\n",
    "    if item % 2 == 0:\n",
    "        print(\"偶数\")\n",
    "    else:\n",
    "        print(\"奇数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Today', 'Sunday']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = ' Today,  is, Sunday'\n",
    "text_list = [s.strip() for s in text.split(',') if len(s.strip()) > 3]\n",
    "text_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##练习题\n",
    "\n",
    "# 输入\n",
    "attributes = ['name', 'dob', 'gender']\n",
    "values = [['jason', '2000-01-01', 'male'], \n",
    "['mike', '1999-01-01', 'male'],\n",
    "['nancy', '2001-02-01', 'female']\n",
    "]\n",
    "\n",
    "# 输出:\n",
    "[{'name': 'jason', 'dob': '2000-01-01', 'gender': 'male'}, \n",
    "{'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'}, \n",
    "{'name': 'nancy', 'dob': '2001-02-01', 'gender': 'female'}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': 'jason', 'dob': '2000-01-01', 'gender': 'male'},\n",
       " {'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'},\n",
       " {'name': 'nancy', 'dob': '2001-02-01', 'gender': 'female'}]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[ dict(zip(attributes,value)) for value in values]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
